/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
//#include "driver/gpio.h"
#include "esp_check.h"
//#include "esp_system.h"
//#include "esp_heap_caps.h"
#include "error_check.h"
#include "t_radio.h"

//#include "rotary_encoder.h"
//#include "chip_info.h"
//#include "display.h"
#include "events.h"



// Function prototypes

static const char* TAG = "app";

void app_main(void)
{
    QueueHandle_t event_queue = xQueueGenericCreate(1, sizeof(event_t), queueQUEUE_TYPE_SET);

    ESP_ERROR_CHECK(t_radio_create(event_queue));

    /*static QueueHandle_t event_queue = NULL;
    uint8_t volume = 12;

    ESP_ERROR_CHECK(print_chip_info());

    rotary_encoder_handle_t rotary_encoder;

    ESP_ERROR_CHECK(display_create());
    
    event_queue = xQueueGenericCreate(1, sizeof(event_t), queueQUEUE_TYPE_SET);

    ESP_ERROR_CHECK(initialize_rotary_encoder(&rotary_encoder, event_queue));
    // Инициализация и запуск задачи обновления дисплея
    ESP_ERROR_CHECK(display_start(event_queue));*/

    while(true)
    {
        event_t event;

        if (pdTRUE == xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000)))
        {
            switch (event.event_type)
            {
                case EVENT_TYPE_ENCODER:
                {
                    uint8_t volume_level;

                    if (ESP_OK == t_radio_get_volume_level(&volume_level))
                    {
                        if ((ENCODER_ACTION_CW == event.generic_encoder.action) && (T_RADIO_VOLUME_MAX > volume_level))
                        {
                            volume_level++;
                            t_radio_set_volume_level(volume_level);
                        }
                        else
                        if ((ENCODER_ACTION_CCW == event.generic_encoder.action) && (T_RADIO_VOLUME_MIN < volume_level))
                        {
                            volume_level--;
                            t_radio_set_volume_level(volume_level);
                        }                        
                    }

                    break;
                }
            }
        }
    }
}

